//
//  Helper.swift
//  IDF
//
//  Created by 陈徐挺 on 22/03/2018.
//  Copyright © 2018 Jesse.chen. All rights reserved.
//

import UIKit
import Dollar
import SwiftyJSON
import AliyunOSSiOS
/**
 调试台打印
 
 - parameter item:
 */
var mainTabBarController: UITabBarController!
var nowNavgationController: UINavigationController!
var appWindow: UIWindow!
var appUser: User!
let kWidth = UIScreen.main.bounds.width
let kHeight = UIScreen.main.bounds.height
let kBounds = UIScreen.main.bounds
var appLogined: Bool = false
var appLaunched: Bool = false
var appSelection: [Selection] = []
var debitStyle: String = ""
var debitPeriod: String = ""
var dailyInterest: String = ""
var payBackMoney: String = ""
var client: OSSClient!
var appConfig: AppConfig!
var registrationId: String = ""
var debitStyleModel: DebitStyle!



func dPrint( _ item:  @autoclosure () -> Any) {
    #if DEBUG
        print(item())
    #endif
}

func initViewContollerWithStoryboard<T: UIViewController>(storyboardName: String, storyboardId: String) -> T {
    let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: storyboardId)
    vc.hidesBottomBarWhenPushed = true
    return vc as! T
}

func getSelection(key: String, selectValue: Int? = nil, selectText: String? = nil) -> Selection? {
    if let value = selectValue {
        let section = Dollar.find(appSelection) { (item) -> Bool in
            return item.selectType == key && item.selectValue == value
        }
        return section
    }
    if let text = selectText {
        let section = Dollar.find(appSelection) { (item) -> Bool in
            return item.selectType == key && item.selectText == text
        }
        return section
    }
    return nil
}

enum Validate {
    case email(String)
    case phoneNum(String)
    case carNum(String)
    case username(String)
    case password(String)
    case nickname(String)
    case URL(String)
    case IP(String)
    
    var isRight: Bool {
        var predicateStr: String!
        var currObject: String!
        switch self {
        case let .email(str):
            predicateStr = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
            currObject = str
        case let .phoneNum(str):
            predicateStr = "^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[06-8])\\d{8}$"
            currObject = str
        case let .carNum(str):
            predicateStr = "^[A-Za-z]{1}[A-Za-z_0-9]{5}$"
            currObject = str
        case let .username(str):
            predicateStr = "^[A-Za-z]{6,20}+$"
            currObject = str
        case let .password(str):
            predicateStr = "^[a-zA-Z0-9]{6,20}+$"
            currObject = str
        case let .nickname(str):
            predicateStr = "^[a-zA-Z0-9\\u4e00-\\u9fa5]{2,64}$"
            currObject = str
        case let .URL(str):
            predicateStr = "^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$"
            currObject = str
        case let .IP(str):
            predicateStr = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
            currObject = str
        }
        
        let predicate =  NSPredicate(format: "SELF MATCHES %@", predicateStr)
        return predicate.evaluate(with: currObject)
    }
}


extension Data {
    var html2AttributedString: NSAttributedString? {
        do {
            return try NSAttributedString(data: self, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
        } catch {
            print("error:", error)
            return  nil
        }
    }
    var html2String: String {
        return html2AttributedString?.string ?? ""
    }
}

extension String {
    var html2AttributedString: NSAttributedString? {
        return Data(utf8).html2AttributedString
    }
    var html2String: String {
        return html2AttributedString?.string ?? ""
    }
}
